/****
	Copyright 2011 Taras Kulbachka

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

		http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/
package com.atdevsoft.apps.abyss;

import android.content.Context;
import android.database.Cursor;
import android.text.Html;
import android.view.View;
import android.widget.TextView;
import android.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.ViewGroup;

public class QuoteAdapter extends SimpleCursorAdapter {
    private static final int TYPE_QUOTE = 0;
    private static final int TYPE_NEXT = 1;
    private static final int TYPE_MAX_COUNT = TYPE_NEXT + 1;
	
    private Context mContext;
    private LayoutInflater mLi;
    public int Count;

    public QuoteAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        mContext = context;        
        mLi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Count = c.getCount();
    }
    
    @Override
    public int getItemViewType(int position) {
        return (position + 1 == Count) && (Count > QuotesActivity.QUOTES_PER_PAGE) ? TYPE_NEXT : TYPE_QUOTE;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_MAX_COUNT;
    }

    private void setView(View v) {
    	Cursor c = getCursor();

        String id = c.getString(c.getColumnIndex(DatabaseHelper.QUOTES_BASH_ID));
        String posted = c.getString(c.getColumnIndex(DatabaseHelper.QUOTES_POSTED));
        String quote = c.getString(c.getColumnIndex(DatabaseHelper.QUOTES_QUOTE));   
        TextView tvId = (TextView)v.findViewById(R.id.tv_id);
        TextView tvPosted = (TextView)v.findViewById(R.id.tv_posted);
        TextView tvQuote = (TextView)v.findViewById(R.id.tv_quote);
        
        tvId.setText(id);
        tvPosted.setText(posted);      
        tvQuote.setText(Html.fromHtml(quote).toString().trim());  
    }
        
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

    	View view = null;		
		int type = getItemViewType(cursor.getPosition());
		switch (type) {
			case TYPE_QUOTE:
				view = mLi.inflate(R.layout.quote, null);
				setView(view);
				break;
			case TYPE_NEXT:
				view = mLi.inflate(R.layout.next_page, null);
				break;
		}            
    	return view;   
    }

    @Override
    public void bindView(View v, Context context, Cursor c) {
		int type = getItemViewType(c.getPosition());        
        if (v == null) {          
            switch (type) {
                case TYPE_QUOTE:
                    v = mLi.inflate(R.layout.quote, null);
                    setView(v);
                    break;
                case TYPE_NEXT:
                	v = mLi.inflate(R.layout.next_page, null);
                    break;
            }            
        } else {
        	if (type == TYPE_QUOTE)
        		setView(v);
        }        
    }
    
    @Override
    public boolean isEnabled(int position) { 
            return (position + 1 == Count) && (Count > QuotesActivity.QUOTES_PER_PAGE); 
    }     

}
